package com.xiaoxi.observermode.observer;

import com.xiaoxi.observermode.generator.NumberGenerator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.observermode.observer
 * @ClassName ConcreateObserver
 * @Description TODO
 * @Date 2023/3/27 16:15
 * @Version 1.0
 */
public class FrameObserver extends Frame implements Observer, ActionListener {

    private TextField textField = new TextField();
    private GraphCanvasObserver canvas = new GraphCanvasObserver();
    private Button cancel = new Button("cancel");

    public FrameObserver(String title) throws HeadlessException {
        super(title);

        setLayout(new BorderLayout());
        setBackground(Color.lightGray);

        textField.setEditable(false);
        canvas.setSize(500, 500);
        add(textField, BorderLayout.NORTH);
        add(canvas, BorderLayout.CENTER);
        add(cancel, BorderLayout.SOUTH);

        cancel.addActionListener(this);

        pack();
        show();
    }

    @Override
    public void update(NumberGenerator numberGenerator) {
        // 更新饼图区域
        textField.setText(numberGenerator.getNumber() + "");
        canvas.update(numberGenerator);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.toString());
        System.exit(0);
    }
}
